﻿//-----------------------------------------------------------------------
// <copyright file="TriggerManager.cs" company="FastNET Services">
//     Copyright (c) 2007-2009. All Rights Reserved.
// </copyright>
// <license>
//     GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings

using System;
using System.Collections.Generic;
using System.Security.Principal;
using FastNET.CleanAct.Client.Interfaces.Managers;
using FastNET.CleanAct.Client.Interfaces.Triggers;

#endregion

namespace FastNET.CleanAct.Client.ClientEngine.Managers.Trigger
{
    /// <summary>
    /// Management class for handling all console related processing
    /// </summary>
    internal class TriggerManager : ITriggerManager
    {
        #region Variables

        private readonly List<ITrigger> _triggers;
        private readonly object _lockObject;
        private bool _enabled;

        #endregion

        #region CTor/DTor

        /// <summary>
        /// Initializes a new instance of the TriggerManager class
        /// </summary>
        public TriggerManager()
        {
            _triggers = new List<ITrigger>();
            _lockObject = new object();
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets a value indicating whether or not the TriggerManager is currently enabled
        /// </summary>
        public bool Enabled
        {
            get
            {
                lock (_lockObject)
                {
                    return _enabled;
                }
            }

            set
            {
                lock (_lockObject)
                {
                    _enabled = value;
                }
            }
        }

        #endregion

        #region public ITrigger Calls

        /// <summary>
        /// Called when a user logs onto the system
        /// </summary>
        /// <param name="user">WindowsIdentity of the user the event that occured</param>
        public void SessionLogon(WindowsIdentity user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            lock (_lockObject)
            {
                if (_enabled == false)
                {
                    return;
                }
            }

            foreach (ITrigger trigger in _triggers)
            {
                trigger.SessionLogon(user);
            }
        }

        /// <summary>
        /// Called when a user logs out of the system
        /// </summary>
        /// <param name="user">WindowsIdentity of the user the event that occured</param>
        public void SessionLogoff(WindowsIdentity user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            lock (_lockObject)
            {
                if (_enabled == false)
                {
                    return;
                }
            }

            foreach (ITrigger trigger in _triggers)
            {
                trigger.SessionLogoff(user);
            }
        }

        /// <summary>
        /// Called when a session is locked
        /// </summary>
        /// <param name="user">WindowsIdentity of the user the event that occured</param>
        public void SessionLocked(WindowsIdentity user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            lock (_lockObject)
            {
                if (_enabled == false)
                {
                    return;
                }
            }

            foreach (ITrigger trigger in _triggers)
            {
                trigger.SessionLocked(user);
            }
        }

        /// <summary>
        /// Called when a session is unlocked
        /// </summary>
        /// <param name="user">WindowsIdentity of the user the event that occured</param>
        public void SessionUnlocked(WindowsIdentity user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            lock (_lockObject)
            {
                if (_enabled == false)
                {
                    return;
                }
            }

            foreach (ITrigger trigger in _triggers)
            {
                trigger.SessionUnlocked(user);
            }
        }

        /// <summary>
        /// Called when the application starts
        /// </summary>
        public void ApplicationStart()
        {
            lock (_lockObject)
            {
                if (_enabled == false)
                {
                    return;
                }
            }

            foreach (ITrigger trigger in _triggers)
            {
                trigger.ApplicationStart();
            }
        }

        /// <summary>
        /// Called when the application stops
        /// </summary>
        public void ApplicationStop()
        {
            lock (_lockObject)
            {
                if (_enabled == false)
                {
                    return;
                }
            }

            foreach (ITrigger trigger in _triggers)
            {
                trigger.ApplicationStop();
            }
        }

        /// <summary>
        /// Called before the execution of the engine starts
        /// </summary>
        public void EngineBeforeExecute()
        {
            lock (_lockObject)
            {
                if (_enabled == false)
                {
                    return;
                }
            }

            foreach (ITrigger trigger in _triggers)
            {
                trigger.EngineBeforeExecute();
            }
        }

        /// <summary>
        /// Called after the execution of the engine finishes
        /// </summary>
        public void EngineAfterExecute()
        {
            lock (_lockObject)
            {
                if (_enabled == false)
                {
                    return;
                }
            }

            foreach (ITrigger trigger in _triggers)
            {
                trigger.EngineAfterExecute();
            }
        }

        #endregion
    }
}